package com.example.service.impl;

import com.example.dto.InventoryPageQueryDTO;
import com.example.dto.InventoryPageQueryDTO2;
import com.example.entity.Inventory;
import com.example.entity.Products;
import com.example.mapper.InventoryDao;
import com.example.result.PageResult;
import com.example.service.InventoryService;
import com.example.variable.ProductAndStoreId;
import com.example.vo.InventoryPageQueryVO;
import com.example.vo.InventoryVO;
import com.example.vo.ProductVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Inventory)表服务实现类
 *
 * @author makejava
 * @since 2024-03-21 17:19:12
 */
@Service("inventoryService")
public class InventoryServiceImpl implements InventoryService {
    @Resource
    private InventoryDao inventoryDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Inventory queryById(Long id) {
        return this.inventoryDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param inventory 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Inventory> queryByPage(Inventory inventory, PageRequest pageRequest) {
        long total = this.inventoryDao.count(inventory);
        return new PageImpl<>(this.inventoryDao.queryAllByLimit(inventory, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param inventory 实例对象
     * @return 实例对象
     */
    @Override
    public Inventory insert(Inventory inventory) {
        if(inventory.getDateOfManufacture()==null || inventory.getShelfLife() == null){
            return null;
        }
        inventory.setStatus(0);
        inventoryDao.insert(inventory);
        return inventory;
    }

    /**
     * 修改数据
     *
     * @param inventory 实例对象
     * @return 实例对象
     */
    @Override
    public Inventory update(Inventory inventory) {
        this.inventoryDao.update(inventory);
        return this.queryById(inventory.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.inventoryDao.deleteById(id) > 0;
    }

    @Override
    public void delete(List<Long> ids) {
        inventoryDao.deleteByIds(ids);
    }

    @Override
    public PageResult pageQuery(InventoryPageQueryDTO inventoryPageQueryDTO) {
        PageHelper.startPage(inventoryPageQueryDTO.getPage(),inventoryPageQueryDTO.getPageSize());
        //下一条sql进行分页，自动加入limit关键字分页
        com.github.pagehelper.Page<Inventory> page = inventoryDao.pageQuery(inventoryPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public PageResult pageQuery2(InventoryPageQueryDTO2 inventoryPageQueryDTO) {
        PageHelper.startPage(inventoryPageQueryDTO.getPage(),inventoryPageQueryDTO.getPageSize());
        //下一条sql进行分页，自动加入limit关键字分页
        com.github.pagehelper.Page<InventoryPageQueryVO> page = inventoryDao.pageQuery2(inventoryPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Inventory inventory = inventoryDao.queryById(id);
        inventory.setStatus(status);
        inventoryDao.update(inventory);
    }

    @Override
    public List<InventoryVO> listWithCategoryId(ProductAndStoreId productAndStoreId) {
        List<InventoryVO> productsList = inventoryDao.listByStoreId(productAndStoreId);

        List<InventoryVO> inventoryVOS = new ArrayList<>();

        for (InventoryVO d : productsList) {
            InventoryVO dishVO = new InventoryVO();
            BeanUtils.copyProperties(d,dishVO);

            inventoryVOS.add(dishVO);
        }

        return inventoryVOS;
    }
}
